package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Shift;
import com.ruoyi.system.domain.bo.ShiftBo;
import com.ruoyi.system.domain.vo.ShiftVo;
import com.ruoyi.system.mapper.ShiftMapper;
import com.ruoyi.system.service.IShiftService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 班次信息Service业务层处理
 *
 * @author ruoyi
 */
@RequiredArgsConstructor
@Service
public class ShiftServiceImpl implements IShiftService {

    private final ShiftMapper baseMapper;

    /**
     * 查询班次信息
     */
    @Override
    public ShiftVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询班次信息列表
     */
    @Override
    public TableDataInfo<ShiftVo> queryPageList(ShiftBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Shift> lqw = buildQueryWrapper(bo);
        Page<ShiftVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询班次信息列表
     */
    @Override
    public List<ShiftVo> queryList(ShiftBo bo) {
        LambdaQueryWrapper<Shift> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Shift> buildQueryWrapper(ShiftBo bo) {
        LambdaQueryWrapper<Shift> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), Shift::getUnit, bo.getUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getPostType()), Shift::getPostType, bo.getPostType());
        lqw.eq(StringUtils.isNotBlank(bo.getPostCategory()), Shift::getPostCategory, bo.getPostCategory());
        lqw.like(StringUtils.isNotBlank(bo.getShiftName()), Shift::getShiftName, bo.getShiftName());
        lqw.eq(StringUtils.isNotBlank(bo.getShiftTimePeriod()), Shift::getShiftTimePeriod, bo.getShiftTimePeriod());
        lqw.eq(bo.getIsEnabled() != null, Shift::getIsEnabled, bo.getIsEnabled());
        return lqw;
    }

    /**
     * 新增班次信息
     */
    @Override
    public Boolean insertByBo(ShiftBo bo) {
        Shift add = new Shift();
        BeanUtils.copyProperties(bo, add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改班次信息
     */
    @Override
    public Boolean updateByBo(ShiftBo bo) {
        Shift update = new Shift();
        BeanUtils.copyProperties(bo, update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量删除班次信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO: 做一些业务上的校验,判断是否允许删除
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 批量新增班次信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchInsertByBo(List<ShiftBo> bos) {
        List<Shift> shifts = new ArrayList<>();
        for (ShiftBo bo : bos) {
            Shift shift = new Shift();
            BeanUtils.copyProperties(bo, shift);
            shifts.add(shift);
        }
        return baseMapper.insertBatch(shifts);
    }
}
